package com.lmpop.createIndex.search;

import com.lmpop.createIndex.searchModel.SearchData;
import com.lmpop.createIndex.searchModel.SearchModel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Hashtable;

public class CategoryattrIndexFiles
{
  public ProperUtil properUtil;
  private String file_path;

  public CategoryattrIndexFiles()
  {
    this.properUtil = new ProperUtil();
    this.file_path = this.properUtil.getFilePathByModel("categoryattr");
  }

  public ArrayList getCategoryAttrListByCatId(String cat_id_para) throws IOException {
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();
    paraList.add(new SearchModel("0", "class_id", cat_id_para));
    return search.getSearchInfo(this.file_path, paraList);
  }

  public String getCatAttrFromStr(String attr_desc) throws IOException
  {
    StringBuffer attrBuffer = new StringBuffer();
    if (!attr_desc.equals(""))
    {
      String[] attrDs = attr_desc.split(",");
      for (int i = 0; i < attrDs.length; i++)
      {
        if (attrDs[i].equals(""))
          continue;
        String[] cb_value = attrDs[i].split("\\|");
        if (cb_value.length <= 1)
          continue;
        String attr_name = getAttrNameById(cb_value[0]);
        attrBuffer.append(attr_name + ":" + cb_value[1] + "| ");
      }

    }

    return attrBuffer.toString();
  }

  public String getAttrNameById(String attr_id)
    throws IOException
  {
    if (attr_id.equals("")) return "";
    SearchData search = new SearchData();
    ArrayList paraList = new ArrayList();
    paraList.add(new SearchModel("0", "attr_id", attr_id));
    ArrayList list = search.getSearchInfo(this.file_path, paraList);
    String attr_name = "";
    if ((list != null) && (list.size() > 0)) {
      Hashtable map = (Hashtable)list.get(0);
      if (map.get("attr_name") != null) attr_name = map.get("attr_name").toString();
    }
    return attr_name;
  }
}